/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.bg;

import javax.microedition.lcdui.Graphics;

import com.aphrodite.ui.widgets.Widget;

/**
 * Pin Stripes are rendered as a background.
 * <p>
 * The width of the stripes and stripe color is customizable.
 * </p>
 * 
 * @author kentelt
 * 
 */
public class PinStripedBackground extends Background {

	private int color1 = 0xffffff;

	private int color2 = 0;

	private short layout = Widget.LAYOUT_HORIZONTAL;
	private int stripWidth = 3;

	public PinStripedBackground() {
		super();
	}

	public void render(Graphics g) {
		int curr = 0;
		int end = layout == Widget.LAYOUT_HORIZONTAL ? height / stripWidth : width / stripWidth;
		if (layout == Widget.LAYOUT_HORIZONTAL)
			for (int i = 0; i < end; i++) {
				g.setColor(i % 2 == 0 ? color1 : color2);
				g.fillRect(0, curr, width, stripWidth);
				curr += stripWidth;
			}
		else {
			for (int i = 0; i < end; i++) {
				g.setColor(i % 2 == 0 ? color1 : color2);
				g.fillRect(curr, 0, stripWidth, height);
				curr += stripWidth;
			}
		}

	}

	/**
	 * 
	 * 
	 * @param layout
	 *            Use Widget.LAYOUT_HORIZONTAL or Widget.LAYOUT_Vertical
	 * 
	 */
	public void setLayout(short layout) {
		this.layout = layout;
	}

	/**
	 * Pin stripe colors
	 * 
	 * @param color1
	 * @param color2
	 */
	public void setPinStripeColor(int color1, int color2) {
		this.color1 = color1;
		this.color2 = color2;

	}

	/**
	 * width of each pin stripe
	 * 
	 * @param width
	 *            default is 3px
	 */
	public void setStripeThickness(int width) {
		this.stripWidth = width;
	}
}
